
package com.boot.springBoot.util;


import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;

import org.apache.commons.lang3.StringUtils;

/**
 * 日期工具类
 * @author Administrator
 *
 */
public class DateUtil {
	
	private static String ymd = "yyyy-MM-dd";
	private static String ymdf = "yyyy-MM-dd HH:mm:ss";
	private static String ymds = "yyyy/MM/dd HH:mm:ss";
	private static String ymdt = "yyyyMMddhhMMss";
	private static String ymdfo = "yyyy-MM-dd HH:mm:ss SSS";
	private static SimpleDateFormat df = new SimpleDateFormat(ymd);
	private static GregorianCalendar calendar = new GregorianCalendar();
	
	
	/**
	 * 得到指定格式的当前时间 
	 * @param format
	 * @return
	 */
	public static String getCurrentTime(String format) {
		if(StringUtils.isBlank(format)){
			format = ymdf;
		}
		return  new SimpleDateFormat(format).format(new Date()); 
	}
	
	/**
	 * 获取指定格式的 日期
	 * @param date
	 * @param format
	 * @return
	 */
	public static String getFormatTime(Date date,String format) {
		if(StringUtils.isBlank(format)){
			format = ymdf;
		}
		return new SimpleDateFormat(format).format(date);
	}
	
	
	/**
	 * 时间差GregorianCalendar
	 */
	public static long getTimePlus(String x,String y) {
		try {
			
			calendar.setTime(df.parse(x));		
			long time1 = calendar.getTimeInMillis();
			calendar.setTime(df.parse(y));
			long time2 = calendar.getTimeInMillis();
			long time3 = System.currentTimeMillis();
			System.out.println("time1=="+time1);
			System.out.println("time2=="+time2);
			System.out.println("time3=="+time3);
			long time = (long) ((time2 - time1)/(1000.00*60*60));
			return time;
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return 0;
		
	}
	

	/**
	 * 时间差    格林治
	 */
	public static long getGLZTimePlus(Date date) {
		Date currentTime = new Date();
		long time = (currentTime.getTime() - date.getTime())/1000*60*60;
		System.out.println("currentTime de time=="+currentTime.getTime());
		return time;
	}
	
	
	public static String addDays(int day) {
		calendar.setTime(new Date());
		//calendar.DAY_OF_YEAR 目前发现这两个区别不大 但业界用DAY_OF_MONTH 比较多
		calendar.add(calendar.DAY_OF_MONTH, day);
		String time = df.format(calendar.getTime());
		return time;

	}
	
	
	/**
	 * 
	    * @Title: yesterday  
	    * @Description: TODO(这里用一句话描述这个方法的作用)  
	    * @param @return    参数  
	    * @return String    返回类型  
	    * @throws
	 */
	public static String yesterday() {
		calendar.setTime(new Date());
		calendar.add(calendar.DAY_OF_MONTH, -1);
		String time = df.format(calendar.getTime());
		return time;
	}
	
	
	
	/**
	 *  日期按天递增
	    * @Title: dateStrToDate  
	    * @Description: TODO(这里用一句话描述这个方法的作用)  
	    * @param @param date
	    * @param @return    参数  
	    * @return Date    返回类型  
	    * @throws
	 */
    public static Date dateStrToDate(String date) {
        Date temp = null;
        try {
            temp = new SimpleDateFormat(ymd).parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return temp;
    }
	
	/**
	 * @return 
	 *   日期递增
	    * @Title: addDay  
	    * @Description: TODO(这里用一句话描述这个方法的作用)  
	    * @param @param date    参数  
	    * @return void    返回类型  
	    * @throws
	 */
	public static String addDay(String date) {
		String yesterday =yesterday();
		System.out.println("昨天=="+yesterday());
		
		int x = 1000;
		calendar.setTime(dateStrToDate(date));	
		for (int i = 0; i < x; i++) {
			if(yesterday.equals(df.format(calendar.getTime()))){
				System.out.println("没任务哦  截止到昨天为止！");
				break;
			}else{	
				
				calendar.add(calendar.DAY_OF_MONTH, i);
				System.out.println("当前是："+df.format(calendar.getTime()));
			}
		}
		return null;
	}
	
	public static void main(String[] args){
		
//		System.out.println("当前时间差=="+getTimePlus("2018-08-18 12:53:02","2018-08-18 20:21:35")+"");
//		System.out.println(getFormatTime(new Date(),ymdfo));
//		System.out.println();
//		System.out.println(addDays(-1));
//		for (int i = 1; i < 1000000; i++) {
//			int x = (int)(Math.random()*100);
//			System.out.println(x);
//			if(x == 99){
//				System.out.println("第"+i+"次，x="+x);
//				return;
//			}
//
//			
//		}
		
//		System.out.println(System.nanoTime());
//		System.out.println(System.currentTimeMillis());
		System.out.println(addDay("2018-9-10"));
	}
}
